/*
 * Created by JFormDesigner on Sat Mar 22 09:30:10 CST 2025
 */

package com.xzp.bams.client;

import com.xzp.bams.entity.*;
import com.xzp.bams.exception.*;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

/**
 * @author 许志鹏
 */
public class BusinessPanel extends JPanel {
    private ATMClient atm;

    public BusinessPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }
public void  initBusiness(){
    idLabelText.setText(atm.account.getId()+"");
    nameLabelText.setText(atm.account.getName());
    balanceLabelText.setText(atm.account.getBalance()+"");
    moneyTextField.setText(0.0+"");

    //透支额度
    if(atm.account instanceof CreditAccount){
        ceilingLabelText.setText(((CreditAccount) atm.account).getCeiling()+"");
    }else{
        ceilingLabelText.setText("该账户不支持透支操作");
    }

    //贷款额度
    if(atm.account instanceof LoanSavingAccount){
        loanLabelText.setText(((LoanSavingAccount) atm.account).getLoanAmount()+"");
    }else if(atm.account instanceof LoanCreditAccount){
        loanLabelText.setText(((LoanCreditAccount) atm.account).getLoanAmount()+"");
    }else{
        loanLabelText.setText("该账户不支持贷款操作");
    }

    //下拉菜单  不同账户类型，展示不同的可操作的项
    if(atm.account instanceof LoanSavingAccount){
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","贷款","还贷"}));
    }else if(atm.account instanceof LoanCreditAccount){
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","修改透支额度","贷款","还贷"}));
    }else if(atm.account instanceof CreditAccount){
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","修改透支额度"}));
    }else if(atm.account instanceof SavingAccount){
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账"}));
    }
}
    private void back(ActionEvent e) {
        //获取当前MainPanel的父窗口（ATMClient），的布局方式，即卡片式布局
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        //指定跳转至主界面
        cardLayout.show(this.getParent() , "mainPanel");

        //清空当前业务页面界面
        clear();
    }
    private void clear(){
        idLabelText.setText("");
        nameLabelText.setText("");
        balanceLabelText.setText("");
        ceilingLabelText.setText("");
        loanLabelText.setText("");
        comboBox.setSelectedIndex(0);
        moneyTextField.setText(0.0+"");
    }
    private void submit(ActionEvent e) {
        Long id = Long.valueOf(idLabelText.getText());
        String moneyText = moneyTextField.getText();
        String select = comboBox.getSelectedItem().toString();

        Account acc = null;
        if(moneyText!=null && !moneyText.equals("")){
            double money = Double.parseDouble(moneyText);
            if(money>0.0){
                switch (select) {
                    case "存款":
                        atm.pw.println("deposit#" + id + "#" + money);
                        atm.pw.flush();
                        try {
                            Object o = atm.ois.readObject();
                            if (o instanceof Account) {
                                acc = (Account) o;
                            } else if (o instanceof String) {
                                JOptionPane.showMessageDialog(this, o);
                                moneyTextField.setText("0.0");
                            }
                        } catch (IOException ex) {
                            // throw new RuntimeException(ex);
                            JOptionPane.showMessageDialog(this, "服务器已关闭");
                        } catch (ClassNotFoundException ex) {
                            throw new RuntimeException(ex);
                        }

                        /*try {
                            acc = atm.bank.deposit(id , money);
                        } catch (QueryException ex) {
                            JOptionPane.showMessageDialog(null , ex.getMessage());
                            moneyTextField.setText("0.0");
                        }*/
                        break;
                    case "取款":
                        String pwd = JOptionPane.showInputDialog(null, "请输入您的密码：");
                        atm.pw.println("withdraw#" + id + "#" + pwd + "#" + money);
                        atm.pw.flush();
                        try {
                            Object o = atm.ois.readObject();
                            if (o instanceof Account) {
                                acc = (Account) o;
                            } else if (o instanceof String) {
                                JOptionPane.showMessageDialog(this, o);
                                moneyTextField.setText("0.0");
                            }
                        } catch (IOException ex) {
                            // throw new RuntimeException(ex);
                            JOptionPane.showMessageDialog(this, "服务器已关闭");
                        } catch (ClassNotFoundException ex) {
                            throw new RuntimeException(ex);
                        }

                        /*String pwd = JOptionPane.showInputDialog(null, "请输入您的密码：");
                        try {
                            acc = atm.bank.withdraw(id, pwd, money);
                        } catch (QueryException | BalanceNotEnoughException ex) {
                            JOptionPane.showMessageDialog(null , ex.getMessage());
                            moneyTextField.setText("0.0");
                        }*/
                        break;
                    case "转账":
                        String toIdStr = JOptionPane.showInputDialog(null, "请输入转入账户：");
                        String password = JOptionPane.showInputDialog(null, "请输入您的密码：");
                        if (toIdStr.matches("\\d+")) {
                            atm.pw.println("transfer#" + id + "#" + password + "#" + toIdStr + "#" + money);
                            atm.pw.flush();
                            try {
                                Object o = atm.ois.readObject();
                                if (o instanceof Account) {
                                    acc = (Account) o;
                                } else if (o instanceof String) {
                                    JOptionPane.showMessageDialog(this, o);
                                    moneyTextField.setText("0.0");
                                }

                            } catch (IOException ex) {
                                // throw new RuntimeException(ex);
                                JOptionPane.showMessageDialog(this, "服务器已关闭");
                            } catch (ClassNotFoundException ex) {
                                throw new RuntimeException(ex);
                            }

                            /*Long to = Long.valueOf(toIdStr);
                            try {
                                boolean flag = atm.bank.transfer(id , password , to , money);
                                if(flag){
                                    acc = atm.bank.login(id , password);
                                }
                            } catch (QueryException | TransferException | BalanceNotEnoughException | LoginException ex) {
                                JOptionPane.showMessageDialog(null , ex.getMessage());
                                moneyTextField.setText("0.0");*/

                        } else {
                            JOptionPane.showMessageDialog(null, "转入账户不合法");
                        }
                        break;
                    case "修改透支额度":
                        pwd = JOptionPane.showInputDialog(null, "请输入您的密码：");
                        atm.pw.println("updateCeiling#" + id + "#" + pwd + "#" + money);
                        atm.pw.flush();
                        try {
                            Object o = atm.ois.readObject();
                            if (o instanceof Account) {
                                acc = (Account) o;
                            } else if (o instanceof String) {
                                JOptionPane.showMessageDialog(this, o);
                                moneyTextField.setText("0.0");
                            }
                        } catch (IOException ex) {
                            // throw new RuntimeException(ex);
                            JOptionPane.showMessageDialog(this, "服务器已关闭");
                        } catch (ClassNotFoundException ex) {
                            throw new RuntimeException(ex);
                        }
                       /* pwd = JOptionPane.showInputDialog(null, "请输入您的密码：");
                        try {
                            acc = atm.bank.updateCeiling(id, pwd, money);
                        } catch (QueryException | TypeException ex) {
                            JOptionPane.showMessageDialog(null , ex.getMessage());
                            moneyTextField.setText("0.0");
                        }*/
                        break;
                    case "贷款":
                        try {
                            atm.pw.println("requestloan#" + id + "#" + money);
                            atm.pw.flush();
                            try {
                                Object o = atm.ois.readObject();
                                if (o instanceof Account) {
                                    acc = (Account) o;
                                } else if (o instanceof String) {
                                    JOptionPane.showMessageDialog(this, o);
                                    moneyTextField.setText("0.0");
                                }

                            } catch (IOException ex) {
                                // throw new RuntimeException(ex);
                                JOptionPane.showMessageDialog(this, "服务器已关闭");
                            } catch (ClassNotFoundException ex) {
                                throw new RuntimeException(ex);
                            }

                        } catch (RuntimeException ex) {
                            throw new RuntimeException(ex);
                        }

                            /*acc = atm.bank.requestLoan(id , money);
                        } catch (QueryException | TypeException | LoanException ex) {
                            JOptionPane.showMessageDialog(null , ex.getMessage());
                            moneyTextField.setText("0.0");
                        }*/
                        /*try {
                            acc = atm.bank.requestLoan(id , money);
                        } catch (QueryException | TypeException | LoanException ex) {
                            JOptionPane.showMessageDialog(null , ex.getMessage());
                            moneyTextField.setText("0.0");
                        }*/
                        break;
                    case "还贷":
                        atm.pw.println("payloan#"+id+"#"+money);
                        atm.pw.flush();
                        try {
                            Object o = atm.ois.readObject();
                            if(o instanceof Account){
                                acc = (Account) o;
                            } else if (o instanceof  String ) {
                                JOptionPane.showMessageDialog(this,o);
                                moneyTextField.setText("0.0");
                            }

                        } catch (IOException ex) {
                            // throw new RuntimeException(ex);
                            JOptionPane.showMessageDialog(this,"服务器已关闭");
                        } catch (ClassNotFoundException ex) {
                            throw new RuntimeException(ex);
                        }

                        /*try {
                            acc = atm.bank.payLoan(id , money);
                        } catch (QueryException | TypeException | LoanException ex) {
                            JOptionPane.showMessageDialog(null , ex.getMessage());
                            moneyTextField.setText("0.0");
                        }*/
                        break;
                }

                //刷新页面：初始化业务界面
                if(acc!=null){
                    atm.account = acc;
                    initBusiness();
                }
            }else{
                JOptionPane.showMessageDialog(null,"操作金额必须>0.0！");
            }
        }else{
            JOptionPane.showMessageDialog(null,"金额必须输入！");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        typelabel2 = new JLabel();
        namelabel3 = new JLabel();
        balancelabel4 = new JLabel();
        ceilinglabel5 = new JLabel();
        label6 = new JLabel();
        labeltext = new JLabel();
        comboBox = new JComboBox<>();
        moneyTextField = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        nameLabelText = new JLabel();
        balanceLabelText = new JLabel();
        ceilingLabelText = new JLabel();
        loanLabelText = new JLabel();
        label2 = new JLabel();
        idLabelText = new JLabel();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u4e1a\u52a1\u754c\u9762");
        add(label1);
        label1.setBounds(150, 20, 105, 35);

        //---- typelabel2 ----
        typelabel2.setText("\u8d26\u6237");
        add(typelabel2);
        typelabel2.setBounds(55, 90, 80, typelabel2.getPreferredSize().height);

        //---- namelabel3 ----
        namelabel3.setText("\u7528\u6237\u540d");
        add(namelabel3);
        namelabel3.setBounds(55, 135, 45, namelabel3.getPreferredSize().height);

        //---- balancelabel4 ----
        balancelabel4.setText("\u4f59\u989d");
        add(balancelabel4);
        balancelabel4.setBounds(55, 175, 40, balancelabel4.getPreferredSize().height);

        //---- ceilinglabel5 ----
        ceilinglabel5.setText("\u4fe1\u7528\u989d\u5ea6");
        add(ceilinglabel5);
        ceilinglabel5.setBounds(55, 215, 70, ceilinglabel5.getPreferredSize().height);

        //---- label6 ----
        label6.setText("\u8d37\u6b3e\u989d");
        add(label6);
        label6.setBounds(55, 265, 75, label6.getPreferredSize().height);
        add(labeltext);
        labeltext.setBounds(170, 85, 80, labeltext.getPreferredSize().height);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u5b58\u6b3e",
            "\u53d6\u6b3e",
            "\u8f6c\u8d26",
            "\u4fee\u6539\u900f\u652f\u989d\u5ea6",
            "\u8d37\u6b3e",
            "\u8fd8\u6b3e"
        }));
        add(comboBox);
        comboBox.setBounds(50, 335, 125, 40);

        //---- moneyTextField ----
        moneyTextField.setText("0.0");
        add(moneyTextField);
        moneyTextField.setBounds(210, 335, 55, 35);

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> submit(e));
        add(button1);
        button1.setBounds(new Rectangle(new Point(75, 410), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u91cd\u7f6e");
        button2.addActionListener(e -> back(e));
        add(button2);
        button2.setBounds(new Rectangle(new Point(210, 410), button2.getPreferredSize()));
        add(nameLabelText);
        nameLabelText.setBounds(170, 130, 60, 20);
        add(balanceLabelText);
        balanceLabelText.setBounds(170, 175, 60, 25);
        add(ceilingLabelText);
        ceilingLabelText.setBounds(165, 215, 75, 25);
        add(loanLabelText);
        loanLabelText.setBounds(165, 260, 75, 25);

        //---- label2 ----
        label2.setText(" ");
        add(label2);
        label2.setBounds(new Rectangle(new Point(180, 95), label2.getPreferredSize()));

        //---- idLabelText ----
        idLabelText.setText(" ");
        add(idLabelText);
        idLabelText.setBounds(145, 90, 180, idLabelText.getPreferredSize().height);

        setPreferredSize(new Dimension(400, 455));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel typelabel2;
    private JLabel namelabel3;
    private JLabel balancelabel4;
    private JLabel ceilinglabel5;
    private JLabel label6;
    private JLabel labeltext;
    private JComboBox<String> comboBox;
    private JTextField moneyTextField;
    private JButton button1;
    private JButton button2;
    private JLabel nameLabelText;
    private JLabel balanceLabelText;
    private JLabel ceilingLabelText;
    private JLabel loanLabelText;
    private JLabel label2;
    private JLabel idLabelText;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
